// pages/exportList/exportList.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileName:'',
    fileSize:'',
    id:'',
    userInfo:{},
    email:'',
    isOver:true,
  },

  emailinput(e){
    this.setData({
      email: e.detail.value.trim()
    })
  },
  svae(){
    var that = this
    var emailPattern = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); 
    if (that.data.email.length == 0) {
      wx.showToast({
        title: "请输入邮箱地址",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!emailPattern.test(that.data.email)) {
      wx.showToast({
        title: "邮箱格式错误",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (that.data.isOver){
      that.setData({
        isOver:false,
      })
   
    app.req({
      url: "wechat/activity/exportParticipationToEmail",
      method: "post",
      data: {
        activityId:that.data.id,
        fileName: that.data.fileName,
        email: that.data.email,
      },
      success: function (res) {
        if (200 == res.code) {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
            that.setData({
              isOver: true,
            })
          }, 1000)
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
         if(that.data.email != that.data.userInfo.mail){
           wx.setStorageSync('mail', that.data.email);
         }
        } else {
          that.setData({
            isOver: true,
          })
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    }
  },
  getUserInfo: function () {
    var that = this;
    var data = {}
    app.req({
      url: "wechat/user/getUserInformation",
      method: "post",
      // data: data,
      success: function (res) {
        if (200 == res.code) {
          that.setData({
            userInfo: res.data,
          });
          var mail = wx.getStorageSync('mail')
          console.log(mail)
          if (mail != '') {
            that.setData({
              email: mail
            })
          } else if (that.data.userInfo.mail != '') {
            that.setData({
              email: that.data.userInfo.mail
            })
          }
          console.log("user", that.data.userInfo)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      fileName: options.fileName,
      id:options.id,
      fileSize: options.fileSize,
    })
    that.getUserInfo()
    app.getUserInfo(function (userInfo) {
      // that.setData({
      //   userInfo: userInfo,
      // })
    })
  //  var mail= wx.getStorageSync('mail')
  //  console.log(mail)
  //   if(mail !=''){
  //     that.setData({
  //       email: mail
  //     })
  //   }else if(that.data.userInfo.mail!=''){
  //     that.setData({
  //       email: that.data.userInfo.mail
  //     })
  //   }
    console.log('名字',that.data.fileName,that.data.userInfo)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})